<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
</head>

<body>
    <!-- 
        需求:
            在修改名字后,延迟一秒全名改变
            修改姓时,全名立即就变
     -->

     <!-- 
         watch 监视属性中可以完成异步操作,此功能 computed 无法实现
      -->
    <div id="demo">
        姓:<input type="text" v-model='firstName'><br>
        名:<input type="text" v-model='lastName'><br>
        全名: <span>{{fullName}}</span><br>
        <button @click='fullName = "li-bai"'>点击改变全名为li-bai</button>
    </div>
    <script>
        Vue.config.productionTip = false;

        new Vue({
            el:'#demo',
            data:{
                firstName:'zhang',
                lastName:'san',
                fullName:''
            },
            watch:{
                firstName:{
                    immediate:true,
                    handler(val){
                        this.fullName = val + '-' + this.lastName
                    }
                },
                lastName(val){
                    //开启定时器,使全名延迟变化
                    setTimeout(()=>{
                        this.fullName = this.firstName + '-' + val
                    },1000) 
                },
                fullName(val){
                    this.fullName = val;
                    let arr = val.split('-');
                    this.firstName = arr[0];
                    this.lastName = arr[1];
                }
            }
        })
    </script>
</body>

</html>